/////////////////////////////////////////////
/**
      = Hello Vocabulary @2010 =
	version 0.2.10
http://code.google.com/p/hello-vocabulary/
******************************************************

Copyright (c) 2010 Mead, laiqinyi@gmail.com


@ MIT License:Licensing
http://www.opensource.org/licenses/mit-license.php
@ Creative Commons License
http://creativecommons.org/licenses/by/2.5/legalcode
**/
//remove, eliminate the duplication item
Array.prototype.distinct = function(){	
   var newArray=[];
    var set = {};
    for (var i = 0,item; i<this.length; i++ ) {
    		item = this[i];    	
        if (!set[item]) {
            newArray.push(item);
            set[item] = true;
        }
    }
    //this = newArray;
    return newArray;
}
//delete the object,just for String type.
Array.prototype.del = function(obj){	
   var newArray=[];   
    for (var i = 0,item; i<this.length; i++ ) {
    	item = this[i];
    	//if(item == null || item typeof "undefinded") // skip it.
        if (obj.toString()!=this[i].toString()) {
            newArray.push(item);           
        }
    }
    //this = newArray;
    return newArray;
}
/////////////////////////////////////////////

/**JSON data form
{"root":["word_new1","new_word_2","something", "other.."],
"title":"url_title",
"ftime":"2010-10-25-12-58",
"ltime":"2010-10-25-12-58"
}
**/
function addWordS(surl,sword){
	//surl = PRE_ID + surl;
	//sword = sword.toLocaleLowerCase();
	var entry = window.localStorage[surl];
	//add item
	if(entry==null){
//		var j_obj = {"root":[sword]};
//		window.localStorage[surl] = JSON.stringify(j_obj);
//		addURL(surl);
		console.log("NULL:"+surl+"="+sword);
		return;//must cause some error.
	}else{
		var list = JSON.parse(entry);
		//console.log("1$update:"+surl+"="+sword+"  #length="+list.root.length);
		list.root.push(sword);
		list.root = list.root.distinct();
		window.localStorage.removeItem(surl);
		window.localStorage[surl]=JSON.stringify(list);
		//console.log("2$update:"+surl+"="+sword+"  #length="+list.root.length);
	}
}
/**JSON data form
{"root":["word_new1","new_word_2","something", "other.."],
"title":"url_title",
"dtime":"2010-10-25-12-58"
}
**/
// Its invoke first time.
function addWord(surl,sword,stitle,stime){
	//surl = PRE_ID + surl;
	//sword = sword.toLocaleLowerCase();
	var entry = window.localStorage[surl];
	//add item
	if(entry==null){
		var j_obj = {"root":[sword],"title":stitle,"ftime":stime,"ltime":stime};
		window.localStorage[surl] = JSON.stringify(j_obj);
		//first time, need add the URL to root.
		addURL(surl);
		//console.log("NULL:"+surl+"="+sword);
	}else{
		addWordS(surl,sword);
	}
}

function getURLProperty(surl){
	var entry = window.localStorage[surl];
	//add item
	if(entry==null){		
		delURL(surl);	///TODO		
		//console.log("NULL:"+surl+"="+sword);
		return "";
	}else{		
		var list = JSON.parse(entry);
		var obj = {};
		obj.title = list.title;
		obj.ftime = list.ftime;
		obj.ltime = list.ltime;
		return obj;
		
	}
}

function delWord(surl,sword){	
	var entry = window.localStorage[surl];
	//add item
	if(entry==null){		
		delURL(surl);	///TODO		
		//console.log("NULL:"+surl+"="+sword);
		return;
	}else{		
		var list = JSON.parse(entry);
		//console.log("1$del:"+surl+"="+sword+"  #length="+list.root.length);		
		list.root = list.root.del(sword);	///TODO					
		if(list.root.length ==0 ){
			delURL(surl);	///TODO		
			window.localStorage.removeItem(surl);
			//console.log("DELETE ALL ");
		}else{
			window.localStorage.removeItem(surl);
			window.localStorage[surl]=JSON.stringify(list);
			//console.log("2$del:"+surl+"="+sword+"  #length="+list.root.length);
		}
	}
}
// Array[]
function getWords(surl){
	var entry = window.localStorage[surl];
	if(entry==null){
		delURL(surl);	///
		return new Array();
	}
	var list = JSON.parse(entry);
	return list.root;
}

//TODO
function getAllURL(){
	//TODO
	var entry = window.localStorage[URL_KEY];
	if(entry==null){
		return;
	}
	var list = JSON.parse(entry);
	//console.log("getAllURL ******URL********* = "+list.root.length);
	//console.log("getAllURL ******URL********* = "+list.root);
	return list.root;
}


function addURL(surl){	
	var entry = window.localStorage[URL_KEY];
	//add item
	if(entry==null){
		var j_obj = {"root":[surl]};
		window.localStorage[URL_KEY] = JSON.stringify(j_obj);
		//console.log("NULL:"+surl+"="+surl);
	}else{
		var list = JSON.parse(entry);
		//console.log("1$ADDURL:"+surl+"****#length="+list.root.length);
		list.root.push(surl);		
		list.root = list.root.distinct();
		//re put it
		window.localStorage.removeItem(URL_KEY);
		window.localStorage[URL_KEY]=JSON.stringify(list);
		//console.log("2$ADDURL:"+surl+"****#length="+list.root.length);
	}
}
//{root:[]}


function delURL(surl){	
	if(surl== null || surl ==""){
		return;
	}	
	var entry = window.localStorage[URL_KEY];	
	//del item
	if(entry==null){
		window.localStorage.removeItem(surl);
		//console.log("NULL:"+surl+"="+surl);
		return;		
	}else{
		var list = JSON.parse(entry);
		//console.log("1$delURL:"+surl+"********#length="+list.root.length);	
		list.root = list.root.del(surl);	///TODO			
		//remove the URL set, if its exist.
		window.localStorage.removeItem(surl);
		//re put it
		window.localStorage.removeItem(URL_KEY);
		window.localStorage[URL_KEY]=JSON.stringify(list);
		//console.log("2$delURL:"+surl+"********#length="+list.root.length);
	}
}

function delAllData(){		
	try{
		window.localStorage.clear();
	}catch(exc){
		console.log("ERROR"+exc);
		return false;
	}
	return true;
}

function clearUpData(){
	var rtn = 0;
	var entry = window.localStorage[URL_KEY];	
	//del item
	if(entry==null){		
		window.localStorage.clear();
		return 99;		
	}else{
		var list = JSON.parse(entry).root;
		var item_d = null;
		for(var ik = 0 ; ik<list.length; ik++){			
			item_d = window.localStorage.getItem(list[ik]);
			if(item_d ==null){
				delURL(list[ik]);
				rtn ++;
			}
			try{
				undefined
				item_d = JSON.parse(item_d);
				if(item_d.title ==null ||item_d.title=="undefined"||item_d.title=="null" 
				   || item_d.root.length==0 ||item_d.root=="undefined"||item_d.root=="null")
				{
					delURL(list[ik]);
				}
			}catch(exc){
				delURL(list[ik]);
				console.log("ERROR @ clearUpData=="+exc);
				rtn ++;
			}finally{
				continue;
			}
		}	
	}
	return rtn;
}


//on=1,off=0
function is_on(){
	var sd = window.localStorage["HV_ON"];
	if(sd==null ){
		window.localStorage["HV_ON"]=1;
		return true;
	}
	return sd;
}

var URL_KEY="MAIN_MEAD_ITEM";